//
//  MonitorCustomCalloutView.swift
//  TJJPT
//
//  Created by FENGYL on 2017/7/25.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit

class MonitorCustomCalloutView: UIView {

    var serialNumber:String?
    var time:String?
    var displayName:String?
    
    var serialNumberLabel:UILabel?
    var timeLabel:UILabel?
    var displayNameLabel:UILabel?
    
    
    let kArrorHeight:CGFloat = 10
    
    let kPortraitMargin = 5
    let kPortraitWidth = 70
    let kPortraitHeight = 50
    
    let kTitleWidth = 300
    let kTitleHeight = 20
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.clear
        self.initSubViews()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }
    
    override func draw(_ rect: CGRect) {
        
        self.drawInContext(context: UIGraphicsGetCurrentContext()!)
        
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOpacity = 1.0
        self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        
    }
    
    func drawInContext(context:CGContext) {
        
        context.setLineWidth(2.0)
        context.setFillColor(UIColor.init(colorLiteralRed: 0.3, green: 0.3, blue: 0.3, alpha: 0.8).cgColor)
        
        self.getDrawPath(context: context)
        context.fillPath()
        
    }
    
    func getDrawPath(context:CGContext) {
        
        
        let rrect = self.bounds;
        let radius:CGFloat = 6.0;
        let minx = rrect.minX
        let midx = rrect.midX
        let maxx = rrect.maxX
        let miny = rrect.minY
        let maxy = rrect.maxY - kArrorHeight
        
        context.move(to: CGPoint(x: midx + kArrorHeight, y: maxy))
        context.addLine(to: CGPoint(x: midx, y: maxy + kArrorHeight))
        context.addLine(to: CGPoint(x: midx - kArrorHeight, y: maxy))
        
        context.addArc(tangent1End: CGPoint(x: minx, y: maxy), tangent2End: CGPoint(x: minx, y: miny), radius: radius)
        context.addArc(tangent1End: CGPoint(x: minx, y: minx), tangent2End: CGPoint(x: maxx, y: miny), radius: radius)
        context.addArc(tangent1End: CGPoint(x: maxx, y: miny), tangent2End: CGPoint(x: maxx, y: maxx), radius: radius)
        context.addArc(tangent1End: CGPoint(x: maxx, y: maxy), tangent2End: CGPoint(x: midx, y: maxy), radius: radius)
        context.closePath()
        
    }
    
    func initSubViews() {
        
        //添加serialNumber
        self.serialNumberLabel = UILabel.init(frame: CGRect(x: kPortraitMargin, y: kPortraitMargin, width: kTitleWidth, height: kTitleHeight))
        self.serialNumberLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        self.serialNumberLabel?.textColor = UIColor.white
        self.serialNumberLabel?.text = "serialNumberLabelserialNumberLabelserialNumberLabel"
        self.addSubview(self.serialNumberLabel!)
        
        //添加名称
        self.displayNameLabel = UILabel.init(frame: CGRect(x: kPortraitMargin, y: kPortraitMargin * 2 + kTitleHeight, width: kTitleWidth, height: kTitleHeight))
        self.displayNameLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        self.displayNameLabel?.textColor = UIColor.white
        self.displayNameLabel?.text = "displayNameLabeldisplayNameLabeldisplayNameLabel"
        self.addSubview(self.displayNameLabel!)
        
        //添加时间
        self.timeLabel = UILabel.init(frame: CGRect(x: kPortraitMargin, y: kPortraitMargin * 3 + kTitleHeight * 2, width: kTitleWidth, height: kTitleHeight))
        self.timeLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        self.timeLabel?.textColor = UIColor.white
        self.timeLabel?.text = "timeLabeltimeLabeltimeLabeltimeLabeltimeLabel"
        self.addSubview(self.timeLabel!)
        
        
        
        
    }
    
    func setUpLabel(serialNumber:String,time:String,displayName:String) {
        
        self.serialNumberLabel?.text = serialNumber
        self.timeLabel?.text = time
        self.displayNameLabel?.text = displayName
        
    }

}
